﻿/* IRCClient
 * 
 * This is a simple IRC client written in the class Computer Networks at Reykjavík University.
 * It was written by:
 * 
 * Karl Karlsson
 * Karl Valdimar Kristinsson
 * Stefán Gunnarsson 
 * Þórður Örn Björnsson
 * 
 * The program takes in 2 parameters from the command line, first the server address and
 * secondly the preferred nickname. Optionally you can also include the port number.
 * 
 * IRCClient.exe server-address nickname port
 * 
 * If that nickname is not available you can assign a new one with the /nick command.
 * For information about available commands use the /help command.
 * 
 */

using System; // For String, Int32, Console, ArgumentException
using System.Text; // For Encoding
using System.Threading;
using System.Collections.Generic;
using System.Net.Sockets; // For TcpClient, NetworkStream, SocketException
using TsamIRC;

class IRCClient
{
    static void Main(string[] args)
    {
        args = new string[2];
        args[0] = "irc.freenode.org";
        args[1] = "tetetememe";
        // Create the client and pass in the command line arguments to the constructor.
        ClientController client = new ClientController(args);

        try
        {
            // Initialize and connect to the server.
            if (!client.initialize())
            {
                // Client failed to connect OR parameters were invalid. The exact reason is
                // reported via console. See ClientController.initialize() for details.
                return; 
            }
            Console.Clear(); // Clear the screen.

            // Create and start the background thread that will take care of all received 
            // data from the IRC server.
            Thread recvThread = new Thread(new ThreadStart(client.receive));
            Thread intervalThread = new Thread(new ThreadStart(client.interval));
            recvThread.Start();
            intervalThread.Start();

            // Main thread will take care of user input.
            client.send();

            // The client has disconnected. Terminate threads.
            recvThread.Abort();
            intervalThread.Abort();
            
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }

        // Permanently close connection and clean up.
        client.cleanup();

    }

}